package com.ywgt.utils;

import com.ywgt.entity.FastDFSFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;
import java.io.*;

public class FastDFSUtil {
    /**
     * 加载配置文件
     */
    static {
        try {
            String filepath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();
            ClientGlobal.init(filepath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 上传文件
     * @param file
     * @return
     * @throws Exception
     */
    public static String fileUpload(FastDFSFile file) throws Exception {
        //创建客户端
        TrackerClient trackerClient=new TrackerClient();
        //获取连接
        TrackerServer trackerServer = trackerClient.getConnection();
        //存储客户端
        StorageClient storageClient=new StorageClient(trackerServer,null);
        //文件相关信息封装类
        NameValuePair[] metaList=new NameValuePair[1];
        metaList[0]=new NameValuePair("作者",file.getAuthor());
        String[] strings = storageClient.upload_file(file.getContent(), file.getExtension(), metaList);
        //获取客户端ip及端口
        String hostString = trackerServer.getInetSocketAddress().getHostString();
        int port = ClientGlobal.getG_tracker_http_port();
        //strings[0]组名 strings[1]存储路径
        return "http://"+hostString+":"+port+"/"+strings[0]+"/"+strings[1];
    }

    /**
     * 下载文件
     * @param groupName
     * @param fileName
     * @return
     * @throws Exception
     */
    public static InputStream fileDownload(String groupName ,String fileName) throws Exception {
        //创建客户端
        TrackerClient trackerClient=new TrackerClient();
        //获取连接
        TrackerServer trackerServer = trackerClient.getConnection();

        StorageClient storageClient=new StorageClient(trackerServer,null);
        //文件相关信息封装类
        byte[] bytes = storageClient.download_file(groupName, fileName);

        return new ByteArrayInputStream(bytes);
    }

    /**
     * 获取文件信息
     * @param groupName
     * @param fileName
     * @return
     * @throws Exception
     */
    public static FileInfo getFileInfo(String groupName,String fileName) throws Exception {
        //创建客户端
        TrackerClient trackerClient=new TrackerClient();
        //获取连接
        TrackerServer trackerServer = trackerClient.getConnection();

        StorageClient storageClient=new StorageClient(trackerServer,null);

        return storageClient.get_file_info(groupName,fileName);
    }

    /**
     * 删除文件
     * @param groupName
     * @param fileName
     * @throws Exception
     */
    public static void deleteFile(String groupName,String fileName) throws Exception{
        //创建客户端
        TrackerClient trackerClient=new TrackerClient();
        //获取连接
        TrackerServer trackerServer = trackerClient.getConnection();

        StorageClient storageClient=new StorageClient(trackerServer,null);

        storageClient.delete_file(groupName,fileName);
    }

}
